package com.ruoyi.lm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.lm.domain.*;
import com.ruoyi.lm.service.*;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 活动Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/lm/report")
public class LmReportController extends BaseController
{
    private String prefix = "lm/report";

    @Autowired
    private ILmActivityService lmActivityService;
    @Autowired
    private ILmUserService lmUserService;
    @Autowired
    private ILmWithdrawrecordService lmWithdrawrecordService;
    @Autowired
    private ILmUserlevelpackageService lmUserlevelpackageService;

    @RequiresPermissions("lm:report:view")
    @GetMapping()
    public String activity()
    {
        return prefix + "/report";
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("lm:report:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(String yearSearch,String monthSearch) throws ParseException {
        //startPage();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
        List<LmReport> list = new ArrayList<>();
        //获取系统日期
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        if(EmptyUtils.isNotEmpty(yearSearch) && EmptyUtils.isNotEmpty(monthSearch)){
            year = Integer.parseInt(yearSearch);
            month = Integer.parseInt(monthSearch)-1;
        }
        List<String> dateList = getDayByMonth(year,month);
        int begin = pageSize*(pageNum-1);
        int end = pageSize*pageNum;
        for(int i=begin;i<dateList.size();i++){
            if(i == end){
                break;
            }
            //获取当天用户量
            LmUser lmUser = new LmUser();
            lmUser.setDateStr(dateList.get(i));
            LmReport temp = new LmReport();
            temp.setUserCount(lmUserService.selectLmUserList(lmUser).size());
            temp.setReportDate(dateList.get(i));

            //获取当天提现总金额
            LmWithdrawrecord lmWithdrawrecord = new LmWithdrawrecord();
            lmWithdrawrecord.setWithdrawStatus("已提现");
            lmWithdrawrecord.setDateStr(dateList.get(i));
            float withdrawSum = 0;
            List<LmWithdrawrecord> lmWithdrawrecordList = lmWithdrawrecordService.selectLmWithdrawrecordList(lmWithdrawrecord);
            for(int j=0;j<lmWithdrawrecordList.size();j++){
                withdrawSum += lmWithdrawrecordList.get(j).getWithdrawSum();
            }
            temp.setWithdrawSum(withdrawSum);
            //获取当天充值总金额
            LmUserlevelpackage lmUserlevelpackage =  new LmUserlevelpackage();
            lmUserlevelpackage.setDateStr(dateList.get(i));
            float packageSum = 0;
            List<LmUserlevelpackage> lmUserlevelpackageList = lmUserlevelpackageService.selectLmUserlevelpackageList(lmUserlevelpackage);
            for(int k=0;k<lmUserlevelpackageList.size();k++){
                packageSum += lmUserlevelpackageList.get(k).getPackagePrice();
            }
            temp.setRechargeSum(packageSum);
            //纯收入
            float chunSum = packageSum -withdrawSum;
            DecimalFormat decimalFormat=new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
            temp.setChunSum(Float.valueOf(decimalFormat.format(chunSum)));
            list.add(temp);
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(dateList.size());
        return rspData;
    }


    /**
     * 查询汇总
     */
    @RequiresPermissions("lm:report:list")
    @PostMapping("/getCount")
    @ResponseBody
    public AjaxResult getCount(String yearSearch,String monthSearch) throws ParseException {
        Map<String,Float> resultMap = new HashMap<>();
        //统计总充值金额
        LmUserlevelpackage lmUserlevelpackage =new LmUserlevelpackage();
        List<LmUserlevelpackage> lmUserlevelpackageList = lmUserlevelpackageService.selectLmUserlevelpackageList(lmUserlevelpackage);
        float packageSum = 0;
        if(lmUserlevelpackageList.size()>0){
            for(LmUserlevelpackage temp:lmUserlevelpackageList){
                packageSum +=temp.getPackagePrice();
            }
        }
        resultMap.put("packageSum",packageSum);
        //统计当月充值金额
        float rechargeSum =0;
        float withdrawSum =0;
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        if(EmptyUtils.isNotEmpty(yearSearch) && !"undefined".equals(yearSearch) && !"undefined".equals(monthSearch)  && EmptyUtils.isNotEmpty(monthSearch)){
            year = Integer.parseInt(yearSearch);
            month = Integer.parseInt(monthSearch)-1;
        }
        List<String> dateList = getDayByMonth(year,month);
        for(int i=0;i<dateList.size();i++){
            //获取当天提现总金额
            LmWithdrawrecord lmWithdrawrecord = new LmWithdrawrecord();
            lmWithdrawrecord.setWithdrawStatus("已提现");
            lmWithdrawrecord.setDateStr(dateList.get(i));
            float dwithdrawSum = 0;
            List<LmWithdrawrecord> lmWithdrawrecordList = lmWithdrawrecordService.selectLmWithdrawrecordList(lmWithdrawrecord);
            for(int j=0;j<lmWithdrawrecordList.size();j++){
                dwithdrawSum += lmWithdrawrecordList.get(j).getWithdrawSum();
            }

            //获取当天充值总金额
            LmUserlevelpackage lmUserlevelpackage1 =  new LmUserlevelpackage();
            lmUserlevelpackage1.setDateStr(dateList.get(i));
            float dpackageSum = 0;
            List<LmUserlevelpackage> lmUserlevelpackageList1 = lmUserlevelpackageService.selectLmUserlevelpackageList(lmUserlevelpackage1);
            for(int k=0;k<lmUserlevelpackageList1.size();k++){
                dpackageSum += lmUserlevelpackageList1.get(k).getPackagePrice();
            }
            rechargeSum +=dpackageSum;
            withdrawSum +=dwithdrawSum;
        }
        resultMap.put("rechargeSum",rechargeSum);
        resultMap.put("withdrawSum",withdrawSum);
        resultMap.put("chunSum",rechargeSum-withdrawSum);
        return  new AjaxResult(AjaxResult.Type.SUCCESS,"200",resultMap);
    }

    public static List<String> getDayByMonth(int yearParam,int monthParam){
        List list = new ArrayList();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.set(yearParam,monthParam,1);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) + 1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String aDate=null;
            if(month<10&&i<10){
                aDate = String.valueOf(year)+"-0"+month+"-0"+i;
            }
            if(month<10&&i>=10){
                aDate = String.valueOf(year)+"-0"+month+"-"+i;
            }
            if(month>=10&&i<10){
                aDate = String.valueOf(year)+"-"+month+"-0"+i;
            }
            if(month>=10&&i>=10){
                aDate = String.valueOf(year)+"-"+month+"-"+i;
            }

            list.add(aDate);
        }
        return list;
    }

    /**
     * 新增活动
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 修改活动
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        LmActivity lmActivity = lmActivityService.selectLmActivityById(id);
        mmap.put("lmActivity", lmActivity);
        return prefix + "/edit";
    }
}
